﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SpaceInvade
{
    class StateManager
    {
        // Delegates to abstract the different Update and Draw phases of the current state;
        private delegate void UpdatePhase();
        private delegate void DrawPhase();
        static private UpdatePhase CurrentUpdatePhase;
        static private DrawPhase CurrentDrawPhase;
        // Flag to see if the game loop should continue and its accompanying get and set attributes (Might not need this, depending on other classes)

        // The modes current level
        private static int CurrentLevel = 1;

        // The chosen game mode
        static private GameState ChosenGameState;
        
        // public constructer, starts the game in the main menu
        public StateManager()
        {
        }

        //Public method to expose the Current phase update loop. These will be acceses through
        // The game loop
        static public void Update()
        {
            CurrentUpdatePhase();
        }
        
        //Public method to expose the Current phase draw loop
        static public void Draw()
        {
            CurrentDrawPhase();
        }

        //method sets up the next level in the game mode
        static public void IncrimentLevel()
        {
            CurrentLevel += 1;
            ChosenGameState.InitializeCurrentLevel(CurrentLevel);
            // Maybe put a method to display an intro to a level, like show the ships and pause for three seconds
        }

        // When a game mode is selected, assign its draw and update methods to the state managers delegates.
        // This code is static because it can be called without the calling class having a handle on this class (ie a GameState doesn't have
        // to hold a StateManager class in order to change the current game state
        static public void SelectMode(GameState State)
        {
            if(ChosenGameState != null)
                ChosenGameState.Dispose();

            ChosenGameState = State;
            ChosenGameState.InitializeCurrentLevel(CurrentLevel);
            CurrentDrawPhase = new DrawPhase(State.Draw);
            CurrentUpdatePhase = new UpdatePhase (State.Update);
        }

        public static void Dispose()
        {
            ChosenGameState.Dispose();
        }
    }
}
